/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */

package org.sbbs.cfg;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * <b>描述:</b><br/>
 * <br/>
 * 
 * 说明描述用途及关键点,算法等
 * <p>
 * <b>创建时间:</b> 2025年3月20日 12:58:27 <br/>
 * <b>作者:</b> Mr.FDC <fdc_messenger@126.com> <br/>
 * 
 * <p>
 * <b>修改记录:</b><br/>
 * <ol>
 * <li>......</li>
 * </ol>
 */



@EnableJpaRepositories(
        basePackages = "org.sbbs",
        entityManagerFactoryRef = "primaryEntityManagerFactory",
        transactionManagerRef = "primaryTransactionManager"
//		,        repositoryFactoryBeanClass = ExpandJpaRepositoryFactoryBean.class
)
@Configuration
public class JpaCfgTwo {

	@Primary
	@Bean("primaryDSProp")
	@ConfigurationProperties(prefix = "spring.datasource.primary")
	public DataSourceProperties primaryDataSourceProperties() {
		return new DataSourceProperties();
	}
    @Primary
    @Bean(name = "primaryJpaProperties")
    @ConfigurationProperties(prefix = "spring.jpa.primary")
    public JpaProperties jpaProperties() {
        return new JpaProperties();
    }

	@Primary
	@Bean(name = "primaryDS")
	public DataSource dataSource(@Qualifier("primaryDSProp") DataSourceProperties dataSourceProperties) {
		return dataSourceProperties.initializeDataSourceBuilder().build();
	}
    @Primary
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("primaryDS") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
	
	
    /**
     * 获取主库实体管理工厂对象
     *
     * @param primaryDataSource 注入名为primaryDataSource的数据源
     * @param jpaProperties 注入名为primaryJpaProperties的jpa配置信息
     * @param builder 注入EntityManagerFactoryBuilder
     * @return 实体管理工厂对象
     */
    @Primary
    @Bean(name = "primaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
            @Qualifier("primaryDS") DataSource primaryDataSource,
            @Qualifier("primaryJpaProperties") JpaProperties jpaProperties,
            EntityManagerFactoryBuilder builder
    ) {
        return builder
                // 设置数据源
                .dataSource(primaryDataSource)
                // 设置jpa配置
                .properties(jpaProperties.getProperties())
                // 设置实体包名
                .packages("org.sbbs")
                // 设置持久化单元名，用于@PersistenceContext注解获取EntityManager时指定数据源
                .persistenceUnit("primaryPersistenceUnit").build();
    }

    /**
     * 获取实体管理对象
     *
     * @param factory 注入名为primaryEntityManagerFactory的bean
     * @return 实体管理对象
     */
    @Primary
    @Bean(name = "primaryEntityManager")
    public EntityManager entityManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) {
        return factory.createEntityManager();
    }

    /**
     * 获取主库事务管理对象
     *
     * @param factory 注入名为primaryEntityManagerFactory的bean
     * @return 事务管理对象
     */
    @Primary
    @Bean(name = "primaryTransactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) {
        return new JpaTransactionManager(factory);
    }


	// // @Primary
	// @Bean("secondaryDSProp")
	// @ConfigurationProperties(prefix = "spring.datasource.secondary")
	// public DataSourceProperties SecondaryDataSourceProperties() {
	// return new DataSourceProperties();
	// }

	// // 主数据源
	// @Bean(name = "primaryDataSource")
	//// @Primary
	//// @ConfigurationProperties(prefix = "spring.datasource.primary")
	// public DataSource primaryDataSource() {
	// return DataSourceBuilder.create().build();
	// }
	// @Primary
//	@Bean(name = "primaryDataSource")
//	public DataSource pdataSource(@Qualifier("primaryDS") DataSourceProperties dataSourceProperties) {
//		return dataSourceProperties.initializeDataSourceBuilder().build();
//	}
//
//	@Bean(name = "secondaryDataSource")
//	public DataSource sdataSource(@Qualifier("secondaryDS") DataSourceProperties dataSourceProperties) {
//		return dataSourceProperties.initializeDataSourceBuilder().build();
//	}
	//
	// // 次数据源
	// @Bean(name = "secondaryDataSource")
	// @ConfigurationProperties(prefix = "spring.datasource.secondary")
	// public DataSource secondaryDataSource() {
	// return DataSourceBuilder.create().build();
	// }

}
